/*
 * FNISVGEditor - JavaScript - based editor for SVG graphics 
 * Copyright (C) 2008 - 2009 Antti Leppä / Foyt
 * http://www.foyt.fi
 * 
 * License: 
 * 
 * Licensed under GNU Lesser General Public License Version 2.1 or later (the "LGPL") 
 * http://www.gnu.org/licenses/lgpl.html
 */
FNISVGEditorClipboardPlugin = Class.create(FNISVGEditorPlugin, {
  initialize : function($super, editorInstance, pluginURL) {
    $super(editorInstance, pluginURL);
    
    this._cutButton = new FNIPushButton( {});
    this._cutButton.setImage(this.getPluginURL() + "/gfx/icons/22x22/actions/edit-cut.png");
    this._cutButton.addListener('buttonClick', this, this._onCutButtonClick);
    this._cutButton.setDisabled(true);
    this._cutButton.setTooltip(SVGLocale.getText("clipboardPlugin_cutButtonTooltip"));
    editorInstance.registerToolbarItem('clipboardCut', this._cutButton);
    
    this._copyButton = new FNIPushButton( {});
    this._copyButton.setImage(this.getPluginURL() + "/gfx/icons/22x22/actions/edit-copy.png");
    this._copyButton.addListener('buttonClick', this, this._onCopyButtonClick);
    this._copyButton.setDisabled(true);
    this._copyButton.setTooltip(SVGLocale.getText("clipboardPlugin_copyButtonTooltip"));
    editorInstance.registerToolbarItem('clipboardCopy', this._copyButton);
    
    this._pasteButton = new FNIPushButton( {});
    this._pasteButton.setImage(this.getPluginURL() + "/gfx/icons/22x22/actions/edit-paste.png");
    this._pasteButton.addListener('buttonClick', this, this._onPasteButtonClick);
    this._pasteButton.setDisabled(true);
    this._pasteButton.setTooltip(SVGLocale.getText("clipboardPlugin_pasteButtonTooltip"));
    editorInstance.registerToolbarItem('clipboardPaste', this._pasteButton);

    this._deleteButton = new FNIPushButton( {});
    this._deleteButton.setImage(this.getPluginURL() + "/gfx/icons/22x22/actions/edit-delete.png");
    this._deleteButton.addListener('buttonClick', this, this._onDeleteButtonClick);
    this._deleteButton.setDisabled(true);
    this._deleteButton.setTooltip(SVGLocale.getText("clipboardPlugin_deleteButtonTooltip"));
    editorInstance.registerToolbarItem('clipboardDelete', this._deleteButton);

    editorInstance.getSelectionManager().addListener("selectionChanged", this, this._onSelectionChanged);

    editorInstance.addListener("keydown", this, this._onKeydown);

    this._clipboard = null;
  },
  getName : function() {
    return "clipboard";
  },
  _onCutButtonClick : function(event) {
    this._clipboard = new Array();
    var selectionManager = this.getEditorInstance().getSelectionManager();
    var elements = selectionManager.getSelectedElements();
    for ( var i = 0; i < elements.length; i++)
      this._clipboard.push(elements[i]);

    selectionManager.clearElementSelection();

    var undoGroup = new FNISVGEditorUndoableEventGroupEvent();
    for ( var i = 0; i < this._clipboard.length; i++) {
      var element = this._clipboard[i];
      this.getEditorInstance().removeElement(element);
      undoGroup.addEvent(new FNISVGEditorUndoableElementRemovedEvent(element));
    }
    
    this._editorInstance.getUndoManager().addUndoableEvent(undoGroup);

    this._pasteButton.setDisabled(false);
  },
  _onCopyButtonClick : function(event) {
    this._clipboard = new Array();
    var selectionManager = this.getEditorInstance().getSelectionManager();
    var elements = selectionManager.getSelectedElements();
    for ( var i = 0; i < elements.length; i++)
      this._clipboard.push(elements[i]);

    this._pasteButton.setDisabled(false);
  },
  _onPasteButtonClick : function(event) {
    var pastedNodes = new Array();

    for ( var i = 0; i < this._clipboard.length; i++) {
      var cbNode = this._clipboard[i];
      var element = cbNode.cloneNode(true);
      this.getEditorInstance().appendSVGElement(element);
      pastedNodes.push(element);
    }

    var elementsBBox = fi.foyt.svg.svgdom.FNISVGDOMUtils.getElementSetScreenBBox(pastedNodes);
    var viewBox = this.getEditorInstance().getViewPort().getViewBox();

    var xCorner = (viewBox.x + (viewBox.width / 2)) - (elementsBBox.width / 2);
    var yCorner = (viewBox.y + (viewBox.height / 2)) - (elementsBBox.height / 2);
    
    this.getEditorInstance().getSelectionManager().clearElementSelection();
    var undoGroup = new FNISVGEditorUndoableEventGroupEvent();
    for ( var i = 0; i < pastedNodes.length; i++) {
      var pastedNode = pastedNodes[i];
      var bbox = fi.foyt.svg.svgdom.FNISVGDOMUtils.getScreenBBox(pastedNode);
      var controller = getSVGElementControllerVault().getElementControllerFor(pastedNode);
      controller.moveTo(pastedNode, xCorner + (bbox.x - elementsBBox.x), yCorner + (bbox.y - elementsBBox.y));
      this.getEditorInstance().getSelectionManager().selectElement(pastedNode);
      undoGroup.addEvent(new FNISVGEditorUndoableElementAddEvent(pastedNode));
    }
    
    this._editorInstance.getUndoManager().addUndoableEvent(undoGroup);
  },
  _onDeleteButtonClick : function(event) {
    var selectionManager = this.getEditorInstance().getSelectionManager();
    var elements = selectionManager.getSelectedElements();
    var undoGroup = new FNISVGEditorUndoableEventGroupEvent();

    for ( var i = 0; i < elements.length; i++) {
      var element = elements[i];
      this.getEditorInstance().removeElement(element);
      undoGroup.addEvent(new FNISVGEditorUndoableElementRemovedEvent(element));
    }
    
    selectionManager.clearElementSelection();
    this._editorInstance.getUndoManager().addUndoableEvent(undoGroup);
  },
  _onSelectionChanged : function(event) {
    var editorInstance = this.getEditorInstance();
    var selectionManager = editorInstance.getSelectionManager();

    var elements = selectionManager.getSelectedElements();
    if (elements.length > 0) {
      this._cutButton.setDisabled(false);
      this._copyButton.setDisabled(false);
      this._deleteButton.setDisabled(false);
    } else {
      this._cutButton.setDisabled(true);
      this._copyButton.setDisabled(true);
      this._deleteButton.setDisabled(true);
    }
  },
  _onKeydown : function(event) {
    if (event.ctrl) {
      switch (event.keyCode) {
      case 67: // C
    break;
  case 88: // X
    break;
  case 86: // V
    break;
  }
} else {
  switch (event.keyCode) {
  case Event.KEY_DELETE:
    break;
  }
}
}
});

document.fire("svgEditorPluginLoaded:clipboard", {
  pluginClass : FNISVGEditorClipboardPlugin
});
